﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using ProjectHopur23.Repositories;
using System.Threading.Tasks;
using ProjectHopur23.Models;

namespace ProjectHopur23.Controllers
{
	public class GameHub : Hub
	{
		#region Variables
		protected IRepository rep = null;
		#endregion

		#region Constructors
		public GameHub(IRepository r)
		{
			rep = r;
		}

		public GameHub()
		{
			rep = new Repository();
		}
		#endregion

		#region Functions
		/// <summary>
		/// This is called from the client and will add a comment to the Comments database, if the comment text is not empty that is.
		/// </summary>
		public void PostComment(string userName, string commentText, string activeGameId)
		{
			if(String.IsNullOrEmpty(commentText) == false)
			{
				Comment theComment = new Comment {
					activeGameId = Convert.ToInt32(activeGameId),
					author = userName,
					text = commentText,
					timePosted = DateTime.Now};

				rep.AddComment(theComment);
				rep.Save();

				string time = theComment.timePosted.ToShortTimeString();
				Clients.Group(activeGameId).postedComment(userName, commentText, time);
			}
		}

		/// <summary>
		/// This little function is called from the client through the signalR framework.
		/// It will update the player list for the other players as people join it.
		/// TODO: write logic that prevents entries getting duplicated if a player who joins was already present before.
		/// </summary>
		public void UpdatePlayerList(string userName, string activeGameId)
		{
			int userScore = rep.GetAllUsers().First(u => u.UserName == userName).score;

			Clients.OthersInGroup(activeGameId).updatedPlayerList(userName, userScore);
		}

		/// <summary>
		/// Courtesy of dabs
		/// </summary>
		public void Join(string activeGameId)
		{
			Groups.Add(Context.ConnectionId, activeGameId);
		}

		/// <summary>
		/// Checks if the user is supposed to move this turn.
		/// </summary>
		public bool IsActivePlayer(int userId, int activeGameId)
		{
			Player thePlayer = (from p in rep.GetAllPlayers()
						where p.activeGameId == activeGameId && p.userId == userId
						select p).FirstOrDefault();

			ActiveGame theGame = (from a in rep.GetAllActiveGames()
						where a.ID == activeGameId
						select a).FirstOrDefault();

			if(thePlayer.playerId == theGame.activePlayerId)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// Switches control to the next active player in the game.
		/// This implementation works for games with any number of players, but there may be more efficient implementations possible for simpler games.
		/// </summary>
		public virtual int SwitchActivePlayer(ActiveGame theGame)
		{
			if(theGame.activePlayerId >= (theGame.numberOfPlayers - 1))
			{
				return 0;
			}
			else
			{
				return theGame.activePlayerId + 1;
			}
		}
		#endregion
	}
}
